<?php

class stPublishToolboxController extends stcontroller_2_1 {

    public function __construct($config) {
        parent::__construct($config);
    }

    public function loadAdminCss() { // load more css custom here
        echo "<link rel='stylesheet' href='" . $this->config['backendCss'] . "prettyphoto/prettyPhoto.css' type='text/css' media='all' />";
        //  echo "<link rel='stylesheet' href='". $this->config['backendCss']. "jquery.tooltip.css' type='text/css' media='all' />";
    }

    public function setConfig($config) {
        $this->config = $config;
        if ($this->actionName == "index") {
            $this->actionName = $config ['default_action'];
        }
        $this->config ['classDir'] = $this->config ['pluginDir'] . 'libs/2.1/classes/';
        $this->config ['backendCss'] = $this->config ['pluginPath'] . 'css/backend/';
        $this->config ['backendScript'] = $this->config ['pluginPath'] . 'scripts/backend/';
        $this->config ['frontendCss'] = $this->config ['pluginPath'] . 'css/frontend/';
        $this->config ['frontendScript'] = $this->config ['pluginPath'] . 'scripts/frontend/';
        $this->config ['imagesUrl'] = $this->config ['pluginPath'] . 'images/';
    }

    

    protected function registerTabMenu() {
        global $current_user;
        if (isset($this->config['admin_tabs'])) {
            $user = get_current_user_id();
            //            $user = get_user_by('id', $user);
            //            if ($user->user_login != $this->config['userAdminName']) {
            //                $this->config['disable_tabs'][] = 'userList';
            //                $this->config['disable_tabs'][] = 'addOn';
            //            }
            if (!user_can($user, 'read_private_pages')) {
                $this->config['disable_tabs'][] = 'setting';
                $this->config['disable_tabs'][] = 'addOn';
            }
            $permission = get_userdata($current_user->ID)->wp_capabilities;
            if (!($permission['administrator'] == 1)) {
                $this->config['disable_tabs'][] = 'setting';
            }
            $this->setTabMenu($this->config['admin_tabs']);
        }
    }

    public function loadAdminScript() { // load more js custom here
        wp_enqueue_script('common');
        wp_enqueue_script('jquery-color');
        wp_admin_css('thickbox');
        wp_print_scripts('post');
        wp_print_scripts('media-upload');
        wp_print_scripts('jquery');
        wp_print_scripts('jquery-ui-core');
        wp_print_scripts('jquery-ui-tabs');
        wp_print_scripts('tiny_mce');
        wp_print_scripts('editor');
        wp_print_scripts('editor-functions');
        add_thickbox();
        wp_tiny_mce();
        wp_admin_css();
        wp_enqueue_script('utils');
        echo '<script src="http://connect.facebook.net/en_US/all.js"></script>';
        // echo '<script type="text/javascript" src="' . $this->config ['backendScript'] . 'jquery-ui-timepicker-addon.js"></script>';
        echo '<script type="text/javascript" src="' . $this->config ['backendScript'] . 'jquery.prettyPhoto.js"></script>';
        // echo '<script type="text/javascript" src="' . $this->config ['backendScript'] . 'lib/jquery.bgiframe.js"></script>';
        // echo '<script type="text/javascript" src="' . $this->config ['backendScript'] . 'lib/jquery.dimensions.js"></script>';
        // echo '<script type="text/javascript" src="' . $this->config ['backendScript'] . 'jquery.tooltip.js"></script>';
    }

    public function customAdminEnqueueScript() {
        parent::customAdminEnqueueScript();
    }

    public function loadCss() {
        //this is function import javascript
        parent::loadCss();
    }

    public function loadScript() {
        //this is function import javascript
        parent::loadScript();
    }
    public function activationPlugin() {
        // put your activation code here
        global $wpdb;
        $wpdb->query("CREATE TABLE `st_publishing` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`name` VARCHAR( 255 ) NOT NULL,publish_type VARCHAR(255) NOT NULL,`username` TEXT NOT NULL ,`type` VARCHAR( 15 ) NOT NULL ,`content` TEXT NOT NULL ,`media` TEXT NOT NULL,`image_upload` TEXT NOT NULL,`image_type` INT(2) ,`thumb` TEXT NOT NULL ,`published` INT( 1 ) NOT NULL ,`time` VARCHAR( 17 ) NOT NULL ,`page` TEXT NOT NULL ,`created_date` VARCHAR( 11 ) NOT NULL ,`last_update` VARCHAR( 20 ) NOT NULL,`user_id` NOT NULL) ENGINE = MYISAM ;");
    }

    public function deactivationPlugin() {
        // put your deactivation code here
        global $wpdb;
    }

    public function registerView() {
        $views = array(
            'menu_child1' => 'default_theme',
            //you can call this action by use these urls:
            // 1.   home_url/menu_child1
            // 2.   home_url/?page=plugin_name&action=menu_child1
            // 3.   home_url/?page=plugin_name&action=menu_child1&ajax=1
            'menu_child2' => 'default_theme'
        );
        $this->setView($views);
    }

    function post_tweet($tweet_text) {
        global $current_user;
        $option = $this->functions->cronjob_get_option('stTwitterToolbox_1_settings', $current_user->ID);
        require_once(WP_PLUGIN_DIR . '/stSocialMediaToolbox/Toolboxes/stTwitterToolbox/libs/tmhOAuth.php');
        $tmhOAuth = new tmhOAuth(array(
                    'consumer_key' => $this->config['twitter_consumer_key'],
                    'consumer_secret' => $this->config['twitter_consumer_secret'],
                    'user_token' => $option['user_token'],
                    'user_secret' => $option['user_secrect'],
                ));

        $code = $tmhOAuth->request('POST', $tmhOAuth->url('1/statuses/update'), array(
                    'status' => $tweet_text
                ));
    }

    public function index() {
        global $current_user, $wpdb;
        $this->view->option = $option = get_option("wall_publish_option");

        $this->view->page = $wpdb->get_results("SELECT * FROM st_likepluginfbpagelist WHERE user_id=" . $current_user->ID);
        $this->view->permission = $this->functions->getPermission('stYoutubeToolbox');
        $this->view->slideshare = $this->functions->getPermission('stSlideShareToolbox');
        $this->view->youtube = $wpdb->get_results("SELECT * FROM wp_st_youtubevideo WHERE user_id IN (SELECT id FROM wp_st_youtube_user WHERE user_id = " . get_current_user_id() . " )");
        $this->view->slide = $wpdb->get_results("SELECT * FROM wp_st_slideshare WHERE user_id IN (SELECT id FROM wp_st_slideshare WHERE user_id = " . get_current_user_id() . " )");

        if (isset($_POST['data'])) {
            define('APP_API_KEY', $this->config['facebook_api_key']);
            define('APP_SECRET_KEY', $this->config['facebook_secret']);
            define('APP_ID', $this->config['facebook_app_id']);
            require_once $this->config ['fileDir'] . '/facebook/facebook.php';
            require_once str_replace('Publish', 'Flickr', $this->config['pluginDir']) . 'stFlickrToolboxclass.php';
            $flickr_option = array(
                'flickr_key' => '1f8711876db0c6f31cf443f1bc75764e',
                'flickr_secret' => '6846a902b08503f4',
                'flickr_user_id' => '34084469@N07',
                'flickr_token' => '72157627219993930-5df0528b6f2ae63e'
            );
            $flickr_config = array(
                'default_flickr_key' => 'f8aa364069fdcf9c3df3bf0e4650ae30',
                'default_flickr_secret' => 'eb0b402f3ddf8b9d',
                'default_flickr_token' => '72157625463576546-4c1ea11aaf1c3563',
                'default_flickr_user_id' => '53713111@N03',
            );
            $flickr = new stFlickrToolboxclass($flickr_config, $flickr_option);
            $facebook = new Facebook(APP_API_KEY, APP_SECRET_KEY, $option['session_key']);
            $facebook->set_user($option['uid'], $option['session_key']);
            $publish = 1;
            //echo $_POST['data']['image_upload'];
            if ($_POST['data']['image_type'] == 1) {
                foreach ($_POST['data']['image_upload'] as $image_upload) {
                    if ($image_upload != '') {
                        $image = explode('/wp-content/', $image_upload);
                        $image = get_bloginfo('url') . '/wp-content/' . $image[1];
                        $array_image[] = $image;
                    }
                }
            }
            if ($_POST['data']['publish'] == 0) {
                $publish = 0;

                $attachment = null;
                if ($_POST['data']['type'] == 'image') {

                    if ($_POST['data']['image_type'] == 0)
                        $attachment = array('name' => stripslashes($_POST['data']['name']), 'media' => array(array('type' => 'image', 'src' => $_POST['data']['media'], 'href' => $_POST['data']['media'])));
                    else {
                        foreach ($array_image as $ar_im) {
                            $media[] = array('type' => 'image', 'src' => $ar_im, 'href' => $ar_im);
                        }
                        $attachment = array('name' => stripslashes($_POST['data']['name']),
                            'media' => $media);
                        $media = array();
                    }
                } elseif ($_POST['data']['type'] == 'video') {
                    $attachment = array('name' => stripslashes($_POST['data']['name']), 'href' => stripslashes($_POST['data']['media']), 'description' => '', 'media' => array(array('type' => 'flash', 'swfsrc' => $_POST['data']['media'], 'imgsrc' => $_POST['data']['image'])));
                } elseif ($_POST['data']['type'] == 'swf' || $_POST['data']['type'] == 'slideshare') {
                    $attachment = array('name' => stripslashes($_POST['data']['name']), 'description' => stripslashes($_POST['data']['description']), 'media' => array(array('type' => 'flash', 'swfsrc' => $_POST['data']['media'], 'imgsrc' => $_POST['data']['image'])));
                }
                $action_links = array();
                if (is_array($_POST['data']['page'])) {
                    foreach ($_POST['data']['page'] as $pa) {
                        if ($facebook->api_client->pages_isAdmin($pa, $option['uid'])) {
                            $facebook_post[] = 'http://www.facebook.com/' . str_replace('_', '/posts/', $facebook->api_client->stream_publish($_POST['data']['content'], $attachment, null, null, $pa));
                        } elseif ($_POST['data']['image_type'] == 1) {
                            foreach ($_POST['data']['image_upload'] as $ar_im) {
                                if ($ar_im != '') {
                                    $array = $facebook->api_client->photos_upload(realpath($ar_im), null, $_POST['data']['content'], $pa);
                                    $facebook_post[] = $array['link'];
                                }
                            }
                        } else {
                            $facebook_post[] = 'http://www.facebook.com/' . str_replace('_', '/posts/', $facebook->api_client->stream_publish($_POST['data']['content'], $attachment, null, $pa));
                        }
                    }
                }
                if (intval($_POST['data']['twitter']) == 1) {
                    //$this->post_tweet($_POST['data']['content']);
                }
                if (intval($_POST['data']['flickr'] == 1)) {
                    foreach ($_POST['data']['image_upload'] as $ar_im) {
                        if ($ar_im != '')
                            $flickr_post[] = 'http://www.flickr.com/photos/' . $flickr_option['flickr_user_id'] . '/' . $flickr->flickr->sync_upload($ar_im, $_POST['data']['name'], $_POST['data']['content'], 'SMT', 1);
                    }
                }
            }
            else {
                $date_array = explode(' ', $_POST['data']['time']);
                $date = explode('-', $date_array[0]);
                $time = explode(':', $date_array[1]);

                $current = mktime($time[0], $time[1], 00, intval($date[1]), intval($date[0]), $date[2]);
                if ($current <= mktime(date_i18n("H"), date_i18n("i"), date_i18n("s"), date_i18n("n"), date_i18n("j"), date_i18n("Y"))) {
                    $publish = 0;
                    $attachment = null;
                    if ($_POST['data']['type'] == 'image') {
                        if ($_POST['data']['image_type'] == 0)
                            $attachment = array('name' => stripslashes($_POST['data']['name']), 'media' => array(array('type' => 'image', 'src' => $_POST['data']['media'], 'href' => $_POST['data']['media'])));
                        else {
                            foreach ($array_image as $ar_im) {
                                $media[] = array('type' => 'image', 'src' => $ar_im, 'href' => $ar_im);
                            }
                            $attachment = array('name' => stripslashes($_POST['data']['name']), 'media' => $media);
                        }
                    } elseif ($_POST['data']['type'] == 'video') {
                        $attachment = array('name' => stripslashes($_POST['data']['name']), 'href' => stripslashes($_POST['data']['media']), 'description' => '', 'media' => array(array('type' => 'flash', 'swfsrc' => $_POST['data']['media'], 'imgsrc' => $_POST['data']['image'])));
                    } elseif ($_POST['data']['type'] == 'swf' || $_POST['data']['type'] == 'slideshare') {
                        $attachment = array('name' => stripslashes($_POST['data']['name']), 'description' => stripslashes($_POST['data']['description']), 'media' => array(array('type' => 'flash', 'swfsrc' => $_POST['data']['media'], 'imgsrc' => $_POST['data']['image'])));
                    }
                    $action_links = array();
                    foreach ($_POST['data']['page'] as $pa) {
                        if ($facebook->api_client->pages_isAdmin($pa, $option['uid'])) {
                            $facebook_post[] = 'http://www.facebook.com/' . str_replace('_', '/posts/', $facebook->api_client->stream_publish($_POST['data']['content'], $attachment, null, null, $pa));
                        } elseif ($_POST['data']['image_type'] == 1) {
                            foreach ($_POST['data']['image_upload'] as $ar_im) {
                                if ($ar_im != '') {
                                    $array = $facebook->api_client->photos_upload(realpath($ar_im), null, $_POST['data']['content'], $pa);
                                    $facebook_post[] = $array['link'];
                                }
                            }
                        } else {
                            $facebook_post[] = 'http://www.facebook.com/' . str_replace('_', '/posts/', $facebook->api_client->stream_publish($_POST['data']['content'], $attachment, null, $pa));
                        }
                    }
                    if (intval($_POST['data']['twitter']) == 1) {
                        //$this->post_tweet($_POST['data']['content']);
                    }
                    if (intval($_POST['data']['flickr'] == 1)) {

                        foreach ($array_image as $ar_im) {
                            if ($ar_im != '')
                                $flickr_post[] = 'http://www.flickr.com/photos/' . $flickr_option['flickr_user_id'] . '/' . $flickr->flickr->sync_upload($ar_im, $_POST['data']['name'], $_POST['data']['content'], 'SMT', 1);
                        }
                    }
                }
            }
            if (isset($_POST['data']['page'])) {
                $data['name'] = $_POST['data']['name'];
                $data['username'] = $current_user->user_login;
                $data['type'] = $_POST['data']['type'];
                $data['publish_type'] = 'facebook';
                $data['content'] = $_POST['data']['content'];
                $data['media'] = $_POST['data']['media'];
                $data['thumb'] = $_POST['data']['image'];
                $data['image_upload'] = json_encode($array_image);
                $data['image_type'] = $_POST['data']['image_type'];
                $data['published'] = $publish;
                $data['id_post'] = json_encode($facebook_post);
                $data['time'] = ($_POST['data']['time'] != '') ? $_POST['data']['time'] : date('d-m-Y H:i');
                $data['page'] = json_encode($_POST['data']['page']);
                $data['created_date'] = date('d-m-Y');
                $data['last_update'] = date('d-m-Y H:i:s');
                $data['user_id'] = $current_user->ID;
                $wpdb->insert('st_publishing', $data);
            }
            if (intval($_POST['data']['twitter']) == 1) {
                $data = array();
                $data['name'] = $_POST['data']['name'];
                $data['username'] = $current_user->user_login;
                $data['type'] = $_POST['data']['type'];
                $data['publish_type'] = 'twitter';
                $data['content'] = $_POST['data']['content'];
                $data['media'] = $_POST['data']['media'];
                $data['thumb'] = $_POST['data']['image'];
                $data['image_upload'] = json_encode($array_image);
                $data['image_type'] = $_POST['data']['image_type'];
                $data['published'] = $publish;
                $data['time'] = ($_POST['data']['time'] != '') ? $_POST['data']['time'] : date('d-m-Y H:i');
                $data['page'] = json_encode($_POST['data']['page']);
                $data['created_date'] = date('d-m-Y');
                $data['last_update'] = date('d-m-Y H:i:s');
                $data['user_id'] = $current_user->ID;
                $wpdb->insert('st_publishing', $data);
            }
            if (intval($_POST['data']['flickr']) == 1) {
                $data = array();
                $data['name'] = $_POST['data']['name'];
                $data['username'] = $current_user->user_login;
                $data['type'] = $_POST['data']['type'];
                $data['publish_type'] = 'flickr';
                $data['id_post'] = json_encode($flickr_post);
                $data['content'] = $_POST['data']['content'];
                $data['media'] = $_POST['data']['media'];
                $data['thumb'] = $_POST['data']['image'];
                $data['image_upload'] = json_encode($array_image);
                $data['image_type'] = $_POST['data']['image_type'];
                $data['published'] = $publish;
                $data['time'] = ($_POST['data']['time'] != '') ? $_POST['data']['time'] : date('d-m-Y H:i');
                $data['page'] = json_encode($_POST['data']['page']);
                $data['created_date'] = date('d-m-Y');
                $data['last_update'] = date('d-m-Y H:i:s');
                $data['user_id'] = $current_user->ID;
                $wpdb->insert('st_publishing', $data);
            }
            $arg = array(
                'status' => 'success',
                'title' => 'Content successfully published ',
                'message' => __('The content was successfully published', $config['plugin_name'])
            );
            echo $this->functions->sendMessage(json_encode($arg), 'index');
            die;
        }
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Publish to social media sites';
    }

    public function dashboard() {
        global $wpdb, $current_user;
        $option = $this->option;
        if (isset($_POST['display'])) {
            $option['publish_display'] = $_POST['display'];
            $this->updateOption($option);
        }
        $this->view->publish_display = $option['publish_display'];
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Dashboard';
        $stFacebookToolbox_model = $this->loadmodel("stPublishing");
        $args = array();
        if (isset($option['publish_display'])) {
            $display = $option['publish_display'];
        } else {
            $display = 0;
        }
        $args['where'] = ' user_id = ' . $current_user->ID . ' AND published = ' . $display;
        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;
        if ($_REQUEST['sort']) {
            $args['order'] = array($sort, $sort_type);
        } else {
            $args['order'] = array('id', 'DESC');
        }
        $args['limit'] = array($start, $limit);
        $args['group_by'] = 'last_update';
        $results = $stFacebookToolbox_model->getRecords($args);
        $total = count($wpdb->get_results("SELECT * FROM st_publishing WHERE  user_id = " . $current_user->ID . " AND published = " . $display . " GROUP BY last_update"));
        $pagination = $this->functions->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'name' => __('Content Title', $this->config['plugin_name']),
            'type' => __('Type', $this->config['plugin_name']),
            'published' => __('Status', $this->config['plugin_name']),
            'time' => __('Time published', $this->config['plugin_name']),
            'last_update' => __('Last update', $this->config['plugin_name']),
            'publish_type' => __('Published to', $this->config['plugin_name']),
            'username' => __('Published by', $this->config['plugin_name']),
        );
        register_column_headers('stFacebookToolbox_stFacebookToolbox', $columns);

        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
        $this->view->wpdb = $wpdb;
    }

    public function list_wall() {
        global $wpdb, $current_user;

        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Wall publishing';
        $this->view->iconNew = 'Publish new content to Facebook';
        $this->view->viewNew = "index";
        $permission = get_userdata($current_user->ID)->wp_capabilities;
        $this->view->permission = $permission['administrator'];
        if (intval($permission['administrator']) == 1 AND $_POST['login'] == 'true') {
            $option['access_token'] = $_POST['access_token'];
            $option['session_key'] = $_POST['session_key'];
            $option['uid'] = $_POST['uid'];
            update_option("wall_publish_option", $option);
        }
        $this->view->logoff = $_GET['logoff'];
        $this->view->option = $option = get_option("wall_publish_option");
        $stFacebookToolbox_model = $this->loadmodel("stPublishing");

        // short action handle
        if ($_REQUEST['short_action'] == 'delete') {

            $wpdb->query("DELETE FROM st_publishing WHERE id = " . $_POST['id'] . " AND user_id = " . $current_user->ID);
            $arg = array(
                'status' => 'success',
                'message' => __('Delete successful', $config['plugin_name'])
            );
            echo $this->functions->sendMessage(json_encode($arg));
        }

        $args = array();
        $args['where'] = ' publish_type="facebook" AND user_id = ' . $current_user->ID;
        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;
        if ($_REQUEST['sort']) {
            $args['order'] = array($sort, $sort_type);
        } else {
            $args['order'] = array('id', 'DESC');
        }
        $args['limit'] = array($start, $limit);

        $results = $stFacebookToolbox_model->getRecords($args);
        $total = $wpdb->get_var("SELECT COUNT(*) FROM st_publishing WHERE  user_id = " . $current_user->ID);
        $pagination = $this->functions->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'name' => __('Name', $this->config['plugin_name']),
            'type' => __('Type', $this->config['plugin_name']),
            'content2' => __('Content', $this->config['plugin_name']),
            'published' => __('Status', $this->config['plugin_name']),
            'time' => __('Time Published', $this->config['plugin_name']),
            'created_date' => __('Created Date', $this->config['plugin_name']),
            'last_update' => __('Last update', $this->config['plugin_name']),
        );
        register_column_headers('stFacebookToolbox_stFacebookToolbox', $columns);

        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
    }

    public function list_twitter() {
        global $wpdb, $current_user;
        $stFacebookToolbox_model = $this->loadmodel("stPublishing");
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Twitter publishing';
        // short action handle
        if ($_REQUEST['short_action'] == 'delete') {

            $wpdb->query("DELETE FROM st_publishing WHERE id = " . $_POST['id'] . " AND user_id = " . $current_user->ID);
            $arg = array(
                'status' => 'success',
                'message' => __('Delete successful', $config['plugin_name'])
            );
            echo $this->functions->sendMessage(json_encode($arg));
        }

        $args = array();
        $args['where'] = ' publish_type="twitter" AND user_id = ' . $current_user->ID;
        $cur_page = $_REQUEST['cur_page'] ? $_REQUEST['cur_page'] : 1;
        $limit = $this->config['per_page'];
        $start = ($cur_page - 1) * $limit;

        $sort = $_REQUEST['sort'] ? $_REQUEST['sort'] : 0;
        $sort_type = $_REQUEST['sort_type'] ? $_REQUEST['sort_type'] : 0;
        if ($_REQUEST['sort']) {
            $args['order'] = array($sort, $sort_type);
        } else {
            $args['order'] = array('id', 'DESC');
        }
        $args['limit'] = array($start, $limit);

        $results = $stFacebookToolbox_model->getRecords($args);
        $total = $wpdb->get_var("SELECT COUNT(*) FROM st_publishing WHERE  user_id = " . $current_user->ID);
        $pagination = $this->functions->pagination($start, $limit, $total, $cur_page, $this->actionName);

        $columns = array(
            'name' => __('Name', $this->config['plugin_name']),
            'content2' => __('Content', $this->config['plugin_name']),
            'published' => __('Status', $this->config['plugin_name']),
            'time' => __('Time Published', $this->config['plugin_name']),
            'created_date' => __('Created Date', $this->config['plugin_name']),
            'last_update' => __('Last update', $this->config['plugin_name']),
        );
        register_column_headers('stFacebookToolbox_stFacebookToolbox', $columns);

        //initialize variable to view
        $this->view->results = $results;
        $this->view->pagination = $pagination;
    }

    public function edit_wall_publish() {
        global $wpdb, $current_user;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Publish to social media sites';
        $this->view->option = $option = get_option("wall_publish_option");
        $this->view->result = $wpdb->get_row("SELECT * FROM st_publishing WHERE user_id=" . $current_user->ID . " AND id=" . $_POST['id']);
        $this->view->page = $wpdb->get_results("SELECT * FROM st_likepluginfbpagelist WHERE user_id=" . $current_user->ID);
        $this->view->permission = $this->functions->getPermission('stYoutubeToolbox');
        $this->view->youtube = $wpdb->get_results("SELECT * FROM wp_st_youtubevideo WHERE user_id IN (SELECT id FROM wp_st_youtube_user WHERE user_id = " . get_current_user_id() . " )");
        if (isset($_POST['data'])) {
            define('APP_API_KEY', $this->config['facebook_api_key']);
            define('APP_SECRET_KEY', $this->config['facebook_secret']);
            define('APP_ID', $this->config['facebook_app_id']);
            require_once $this->config ['fileDir'] . '/facebook/facebook.php';
            $facebook = new Facebook(APP_API_KEY, APP_SECRET_KEY, $option['session_key']);
            $facebook->set_user($option['uid'], $option['session_key']);
            $publish = 1;
            if ($_POST['data']['image_upload'] != '') {
                $image = explode('/wp-content/', $_POST['data']['image_upload']);
                $image = get_bloginfo('url') . '/wp-content/' . $image[1];
            }
            if ($_POST['data']['publish'] == 0) {
                $publish = 0;
                $attachment = null;
                if ($_POST['data']['type'] == 'image') {
                    if ($_POST['data']['image_type'] == 0)
                        $attachment = array('name' => stripslashes($_POST['data']['name']), 'media' => array(array('type' => 'image', 'src' => $_POST['data']['media'], 'href' => $_POST['data']['media'])));
                    else
                        $attachment = array('name' => stripslashes($_POST['data']['name']), 'media' => array(array('type' => 'image', 'src' => $image, 'href' => $image)));
                }
                elseif ($_POST['data']['type'] == 'video') {
                    $attachment = array('name' => stripslashes($_POST['data']['name']), 'href' => stripslashes($_POST['data']['media']), 'description' => '', 'media' => array(array('type' => 'flash', 'swfsrc' => $_POST['data']['media'], 'imgsrc' => $_POST['data']['image'])));
                } elseif ($_POST['data']['type'] == 'swf' || $_POST['data']['type'] == 'slideshare') {
                    $attachment = array('name' => stripslashes($_POST['data']['name']), 'description' => stripslashes($_POST['data']['description']), 'media' => array(array('type' => 'flash', 'swfsrc' => $_POST['data']['media'], 'imgsrc' => $_POST['data']['image'])));
                }
                foreach ($_POST['data']['page'] as $pa) {
                    if ($facebook->api_client->pages_isAdmin($pa, $option['uid'])) {
                        $facebook->api_client->stream_publish($_POST['data']['content'], $attachment, null, null, $pa);
                    } else {
                        $facebook->api_client->stream_publish($_POST['data']['content'], $attachment, null, $pa);
                    }
                }
            } else {
                $date_array = explode(' ', $_POST['data']['time']);
                $date = explode('-', $date_array[0]);
                $time = explode(':', $date_array[1]);
                $current = mktime($time[0], $time[1], 00, intval($date[1]), intval($date[0]), $date[2]);
                if ($current <= mktime(date_i18n("H"), date_i18n("i"), date_i18n("s"), date_i18n("n"), date_i18n("j"), date_i18n("Y"))) {
                    $publish = 0;
                    $attachment = null;
                    if ($_POST['data']['type'] == 'image') {
                        if ($_POST['data']['image_type'] == 0)
                            $attachment = array('name' => stripslashes($_POST['data']['name']), 'media' => array(array('type' => 'image', 'src' => $_POST['data']['media'], 'href' => $_POST['data']['media'])));
                        else
                            $attachment = array('name' => stripslashes($_POST['data']['name']), 'media' => array(array('type' => 'image', 'src' => $image, 'href' => $image)));
                    }
                    elseif ($_POST['data']['type'] == 'video') {
                        $attachment = array('name' => stripslashes($_POST['data']['name']), 'href' => stripslashes($_POST['data']['media']), 'description' => '', 'media' => array(array('type' => 'flash', 'swfsrc' => $_POST['data']['media'], 'imgsrc' => $_POST['data']['image'])));
                    } elseif ($_POST['data']['type'] == 'swf') {
                        $attachment = array('name' => stripslashes($_POST['data']['name']), 'description' => stripslashes($_POST['data']['description']), 'media' => array(array('type' => 'flash', 'swfsrc' => $_POST['data']['media'], 'imgsrc' => $_POST['data']['image'])));
                    }
                    foreach ($_POST['data']['page'] as $pa) {
                        if ($facebook->api_client->pages_isAdmin($pa, $option['uid'])) {
                            $facebook->api_client->stream_publish($_POST['data']['content'], $attachment, null, null, $pa);
                        } else {
                            $facebook->api_client->stream_publish($_POST['data']['content'], $attachment, null, $pa);
                        }
                    }
                }
            }
            $data['name'] = $_POST['data']['name'];
            $data['username'] = $current_user->user_login;
            $data['type'] = $_POST['data']['type'];
            $data['publish_type'] = 'facebook';
            $data['content'] = $_POST['data']['content'];
            $data['media'] = $_POST['data']['media'];
            $data['thumb'] = $_POST['data']['image'];
            $data['image_upload'] = $image;
            $data['image_type'] = $_POST['data']['image_type'];
            $data['published'] = $publish;
            $data['time'] = ($_POST['data']['time'] != '') ? $_POST['data']['time'] : date('d-m-Y H:i');
            $data['page'] = json_encode($_POST['data']['page']);
            //$data['created_date'] = date('d-m-Y');
            $data['last_update'] = date('d-m-Y H:m:s');
            $data['user_id'] = $current_user->ID;
            $wpdb->update('st_publishing', $data, array('id' => $_POST['data']['id']));
            $arg = array(
                'status' => 'success',
                'message' => __('Publish successful', $config['plugin_name'])
            );
            echo $this->functions->sendMessage(json_encode($arg));
            die;
        }
    }

    public function setting() {
        global $wpdb, $current_user;
        $this->view->headIcon = 'icon_re';
        $this->view->headTitle1 = 'Setting';
        $permission = get_userdata($current_user->ID)->wp_capabilities;
        $this->view->permission = $permission['administrator'];
        if (intval($permission['administrator']) == 1 AND $_POST['login'] == 'true') {
            $option['access_token'] = $_POST['access_token'];
            $option['session_key'] = $_POST['session_key'];
            $option['uid'] = $_POST['uid'];
            update_option("wall_publish_option", $option);
        }
        $this->view->logoff = $_GET['logoff'];
        $this->view->option = $option = get_option("wall_publish_option");
    }

    public function test() {
        require_once str_replace('Publish', 'Flickr', $this->config['pluginDir']) . 'stFlickrToolboxclass.php';
        $flickr_option = array(
            'flickr_key' => '1f8711876db0c6f31cf443f1bc75764e',
            'flickr_secret' => '6846a902b08503f4',
            'flickr_user_id' => '34084469@N07',
            'flickr_token' => '72157627219993930-5df0528b6f2ae63e'
        );
        $flickr_config = array(
            'default_flickr_key' => 'f8aa364069fdcf9c3df3bf0e4650ae30',
            'default_flickr_secret' => 'eb0b402f3ddf8b9d',
            'default_flickr_token' => '72157625463576546-4c1ea11aaf1c3563',
            'default_flickr_user_id' => '53713111@N03',
        );
        $flickr = new stFlickrToolboxclass($flickr_config, $flickr_option);
        print_r($flickr);
        $id = $flickr->flickr->sync_upload('/var/www/wp-content/uploads/2011/07/41-ok2.png', 'test', 'test', 'SMT', 1);
        echo $id;
    }

    public function publish_popup() {
        global $wpdb, $current_user;
        $this->view->option = $option = get_option("wall_publish_option");
        $this->view->page = $wpdb->get_results("SELECT * FROM st_likepluginfbpagelist WHERE user_id=" . $current_user->ID);
        $this->view->type = $_POST['type'];
        $this->view->pic = $_POST['pic'];
        $this->view->title = $_POST['title'];
        if ($_POST['type'] == 'youtube' || $_POST['type'] == 'slideshare') {
            $this->view->url = $_POST['url'];
        }

        define('APP_API_KEY', $this->config['facebook_api_key']);
        define('APP_SECRET_KEY', $this->config['facebook_secret']);
        define('APP_ID', $this->config['facebook_app_id']);
        require_once $this->config ['fileDir'] . '/facebook/facebook.php';
        $facebook = new Facebook(APP_API_KEY, APP_SECRET_KEY, $option['session_key']);
        $facebook->set_user($option['uid'], $option['session_key']);
        if (isset($_POST['data'])) {
            $publish = 1;
            if ($_POST['data']['type'] == 'youtube') {
                $attachment = array('name' => stripslashes($_POST['data']['content']), 'description' => stripslashes($_POST['data']['description']), 'media' => array(array('type' => 'flash', 'swfsrc' => "http://www.youtube.com/v/" . $_POST['data']['url'], 'imgsrc' => $_POST['data']['img'])));
                $type = 'swf';
                $url = 'http://youtube.com/v/' . $_POST['data']['url'];
            } elseif ($_POST['data']['type'] == 'flick') {
                $attachment = array('name' => stripslashes($_POST['data']['content']), 'media' => array(array('type' => 'image', 'src' => $_POST['data']['img'], 'href' => $_POST['data']['img'])));
                $type = 'image';
                $url = $_POST['data']['url'];
            } elseif ($_POST['data']['type'] == 'slideshare') {
                $attachment = array('name' => stripslashes($_POST['data']['content']), 'description' => stripslashes($_POST['data']['description']), 'media' => array(array('type' => 'flash', 'swfsrc' => $_POST['data']['url'], 'imgsrc' => $_POST['data']['img'])));
                $type = 'swf';
                $url = $_POST['data']['url'];
            }
            if ($_POST['data']['publish'] == 0) {
                $publish = 0;
                foreach ($_POST['data']['page'] as $pa) {
                    if ($facebook->api_client->pages_isAdmin($pa, $option['uid'])) {
                        $facebook->api_client->stream_publish($_POST['data']['name'], $attachment, null, null, $pa);
                    } else {
                        $facebook->api_client->stream_publish($_POST['data']['name'], $attachment, null, $pa);
                    }
                }
            } else {

                $date_array = explode(' ', $_POST['data']['time']);
                $date = explode('-', $date_array[0]);
                $time = explode(':', $date_array[1]);
                $current = mktime($time[0], $time[1], 00, intval($date[1]), intval($date[0]), $date[2]);
                if ($current <= mktime(date_i18n("H"), date_i18n("i"), date_i18n("s"), date_i18n("n"), date_i18n("j"), date_i18n("Y"))) {
                    $publish = 0;
                    foreach ($_POST['data']['page'] as $pa) {
                        if ($facebook->api_client->pages_isAdmin($pa, $option['uid'])) {
                            $facebook->api_client->stream_publish($_POST['data']['name'], $attachment, null, null, $pa);
                        } else {
                            $facebook->api_client->stream_publish($_POST['data']['name'], $attachment, null, $pa);
                        }
                    }
                }
            }
            $data['name'] = $_POST['data']['name'];
            $data['username'] = $current_user->user_login;
            $data['type'] = $type;
            $data['publish_type'] = 'facebook';
            $data['content'] = $_POST['data']['content'];
            $data['media'] = $url;
            $data['thumb'] = $_POST['data']['img'];
            $data['image_type'] = 0;
            $data['published'] = $publish;
            $data['time'] = ($_POST['data']['time'] != '') ? $_POST['data']['time'] : date('d-m-Y H:i');
            $data['page'] = json_encode($_POST['data']['page']);
            $data['created_date'] = date('d-m-Y');
            $data['last_update'] = date('d-m-Y h:m:s');
            $data['user_id'] = $current_user->ID;
            $wpdb->insert('st_publishing', $data);
            $arg = array(
                'status' => 'success',
                'message' => __('Publish successful', $config['plugin_name'])
            );
            echo $this->functions->sendMessage(json_encode($arg));
            die;
        }
        //$facebook->api_client->links_post('http://www.youtube.com/watch?v=E8ZrPFMr_nY', 'hehe',"142912649109324");
    }

    public function twitter_option($option, $user_id = 0, $default = false) {
        global $wpdb;

        if (false) {
            $optionTable = $wpdb->options;
            $where = 'option_name = %s LIMIT 1';
        } else {
            $optionTable = 'wp_st_options';
            if ($user_id == 0)
                $user_id = get_current_user_id();
            $where = "user_id = $user_id AND option_name = %s LIMIT 1";


            $option = trim($option);
            if (empty($option))
                return false;

            if (defined('WP_SETUP_CONFIG'))
                return false;
            $value = wp_cache_get($option, 'options');
            if (false === $value) {
                $suppress = $wpdb->suppress_errors();
                $row = $wpdb->get_row($wpdb->prepare("SELECT option_value FROM $optionTable WHERE {$where} ", $option));
                $wpdb->suppress_errors($suppress);
                if (is_object($row)) {
                    $value = $row->option_value;
                    wp_cache_add($option, $value, 'options');
                }else
                    return $default;
            }

            return apply_filters('option_' . $option, maybe_unserialize($value));
        }
    }

}

?>